package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    /**
     *  ？ 在存在字段为null的时候是否会报错异常
     */
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public List<ShoppingCart> getList() {
        List<ShoppingCart> list = shoppingCartMapper.list();
        return list;
    }

    /**
     * TODO 判断dto中的套餐与菜品id是否为空，
     * 然后进行相应的菜品获取操作
     * 可以通过pojo类的方式获取参数是否在购物车存在存在则更新其数量和amount金额
     * 一一添加，一一判定
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.listByClass(shoppingCart);
        // 当购物车存在该菜品或套餐时
        if (list != null && list.size() == 1){
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //当套餐不存在该菜品或套餐时
            // 判断当前为套餐还是菜品
            Long id = shoppingCart.getDishId();
            // id为null则为套餐
            if (id == null){
                Setmeal setmeal = setMealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
            }else {
                Dish dish = dishMapper.getById(id);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(shoppingCart);
        }
    }

    @Override
    public void clean(Long userId) {
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.listByClass(shoppingCart);
        // 当购物车存在该菜品或套餐时
        if (list != null && list.size() == 1){
            int number = list.get(0).getNumber();
            if (number > 1){
                shoppingCart.setNumber(number - 1);
                shoppingCartMapper.update(shoppingCart);
            }else if (number == 1){
                shoppingCartMapper.deleteByDishIdOr(shoppingCart);
            }
        }
    }
}
